<template>
	<view>
		<uni-card>
			<u-row customStyle="margin-bottom: 10px">
				<u-col span="2">
					<text>头像</text>
				</u-col>
				<u-col span="10">
					<u--image 
						width="64"
						height="64"
						radius="50%"
						:src="userData.userPhoto" 
						shape="circle"
						mode="aspectFill"
						@click="upload()"
					></u--image>
					<!-- <view class="example-body custom-image-box">
						<uni-file-picker 
							limit="1" 
							:del-icon="false" 
							disable-preview 
							:imageStyles="imageStyles"
							file-mediatype="image"
							v-model="imageValue" 
						>选择</uni-file-picker>
					</view> -->
				</u-col>
			</u-row>
		</uni-card>
		<uni-card>
			<u-row customStyle="margin-bottom: 10px">
				<u-col span="2">
					<text>用户名</text>
				</u-col>
				<u-col span="10">
					<u--input
					   placeholder="请输入内容"
					   border="bottom"
					   clearable
					   v-model="userData.userUname"
					 ></u--input>
				</u-col>
			</u-row>
			<u-row customStyle="margin-bottom: 10px">
				<u-col span="2">
					<text>密码</text>
				</u-col>
				<u-col span="10">
					<u--input
					   placeholder="请输入内容"
					   border="bottom"
					   clearable
					   v-model="userData.userPwd"
					 ></u--input>
				</u-col>
			</u-row>
			<u-row customStyle="margin-bottom: 10px">
				<u-col span="2">
					<text>手机号</text>
				</u-col>
				<u-col span="10">
					<u--input
					   placeholder="请输入内容"
					   border="bottom"
					   clearable
					   v-model="userData.userPhone"
					 ></u--input>
				</u-col>
			</u-row>
			<u-row customStyle="margin-bottom: 10px">
				<u-col span="2">
					<text>性别</text>
				</u-col>
				<u-col span="10">
					<u-action-sheet 
						:actions="sexList" 
						:show="sexShow"
						cancelText="取消"
						@close="sheetClose()"
						@select="sexSelectClick"
						v-model="userData.userSex"
					></u-action-sheet>
					<u-button @click="sexShow = true">
						<u--text type="info" :text="userData.userSex==1 ? '男':'女'"></u--text>
					</u-button>
				</u-col>
			</u-row>
			<u-row customStyle="margin-bottom: 10px">
				<u-col span="2">
					<text>年龄</text>
				</u-col>
				<u-col span="10">
					<u--input
					   placeholder="请输入内容"
					   border="bottom"
					   clearable
					   v-model="userData.userAge"
					 ></u--input>
				</u-col>
			</u-row>
			<u-row customStyle="margin-bottom: 10px">
				<u-col span="2">
					<text>学历</text>
				</u-col>
				<u-col span="10">
					<u-action-sheet 
						:actions="eduList" 
						:show="eduShow"
						cancelText="取消"
						@close="sheetClose()"
						@select="eduSelectClick"
						v-model="userData.userEdu"
					></u-action-sheet>
					<u-button @click="eduShow = true">
						<u--text type="info" :text="edu"></u--text>
					</u-button>
				</u-col>
			</u-row>
			<u-row customStyle="margin-bottom: 10px">
				<u-col span="2">
					<text>邮箱</text>
				</u-col>
				<u-col span="10">
					<u--input
					   placeholder="请输入内容"
					   border="bottom"
					   clearable
					   v-model="userData.userEmail"
					 ></u--input>
				</u-col>
			</u-row>
			<u-row customStyle="margin-bottom: 10px">
				<u-col span="2">
					<text>个性签名</text>
				</u-col>
				<u-col span="10">
					<u--input
					   placeholder="请输入内容"
					   border="bottom"
					   clearable
					   v-model="userData.userLabel"
					 ></u--input>
				</u-col>
			</u-row>
			<u-row customStyle="margin-bottom: 10px" v-if="stuData.id!=undefined">
				<u-col span="2">
					<text>现住址</text>
				</u-col>
				<u-col span="10">
					<u--input
					   placeholder="请输入内容"
					   border="bottom"
					   clearable
					   v-model="userData.addressId"
					 ></u--input>
				</u-col>
			</u-row>
		</uni-card>
			
		<uni-card title="班级信息">
			<u-row customStyle="margin-bottom: 10px">
				<u-col span="2">
					<text>班级</text>
				</u-col>
				<u-col span="10">
					<u--input
					   placeholder="无"
					   border="bottom"
					   clearable
					   :disabled="clsShow"
					   v-model="clsData.clsName"
					 ></u--input>
				</u-col>
			</u-row>
			<u-row customStyle="margin-bottom: 10px">
				<u-col span="2">
					<text>阶段</text>
				</u-col>
				<u-col span="10">
					<u--input
					   placeholder="无"
					   border="bottom"
					    :disabled="clsShow"
					   clearable
					   v-model="clsData.stage"
					 ></u--input>
				</u-col>
			</u-row>
			<u-row customStyle="margin-bottom: 10px">
				<u-col span="2">
					<text>专业</text>
				</u-col>
				<u-col span="10">
					<u--input
					   placeholder="无"
					   border="bottom"
						:disabled="clsShow"
					   clearable
					   v-model="clsData.major"
					 ></u--input>
				</u-col>
			</u-row>
		</uni-card>	
	</view>
	
</template>

<script>
	export default {
		onNavigationBarButtonTap() {
		    this.saveData()
		},
		data() {
			return {
				imageValue:[
					{
						"name":"photo.jpg",
						"extname":"jpg",
						"url":"",
					}
				],
				usersUrl:{
					getEduListUrl:"/education/education/educationList",
					getClsBystuIdHashUrl:"/stumiddlecls/stuMiddleCls/getClsBystuId",
					editStuUrl:"/stu/stu/edit",
					editUserUrl:"/users/userInfo/edit",
				},
				stuData:{},
				userData:{},
				clsData:{clsName:"",stage:"",major:""},
				sexShow: false,
				eduShow: false,
				clsShow: true,
				sexList: [
					{name:'男',sexValue:0},
					{name:'女',sexValue:1}
				],
				eduList:[{name:""}],
				edu:""
			}
		},
		created() {
			const user = uni.getStorageSync("user");
			const stu = uni.getStorageSync("stu");
			this.imageValue[0].url=(user.userPhoto)
			if(stu.id!=undefined){
				this.userData=user
				this.stuData=stu
			}else{
				if(user.id!=undefined){
					this.userData=user
				}
			}
			/* 学历 */
			this.$axios.get(this.usersUrl.getEduListUrl).then((res)=>{
				if(res.data.success){
					this.eduList=res.data.result
					for (let i = 0; i < this.eduList.length; i++) {
						this.eduList[i].name=this.eduList[i].eduName
						if(this.eduList[i].eduId==this.userData.userEdu){
							this.edu = this.eduList[i].name
						}
					}
					
				}
			})
			/* 班级信息 */
			this.$axios.get(this.usersUrl.getClsBystuIdHashUrl+"?stuId="+this.stuData.stuId).then((res)=>{
				if(res.data.success){
					this.clsData.major=JSON.parse(JSON.stringify(res.data.result)).major.majorName
					this.clsData.clsName=JSON.parse(JSON.stringify(res.data.result)).clsData.className
					this.clsData.stage=JSON.parse(JSON.stringify(res.data.result)).classStage.classStageName
				}
			})
		},
		methods: {
			upload(){
				/* 将this赋值_this */
				let _this=this
				/* 上传方法 */
				uni.chooseImage({
					/* 上传数量 */
					count: 1,
					/* 上传成功 */
					success(res) {
						/* 判断是否含有文件 */
						if (res.tempFilePaths.length > 0) {
							/* 上传地址 */
							let filePath = res.tempFilePaths[0]
							//进行上传操作
							uniCloud.uploadFile({
								filePath: filePath,
								/* 文件名 */
								cloudPath: res.tempFiles[0].name,
								onUploadProgress: function(progressEvent) {
									/* 文件id */
									var percentCompleted = Math.round(
									(progressEvent.loaded * 100) / progressEvent.total
									);
								},
								/* 上传成功 */
								success(res) {
									/* 回调图片地址 */
									_this.userData.userPhoto=res.fileID
								},
								/* 上传失败 */
								fail() {
									uni.showToast({
										icon: 'none',
										position: 'bottom',
										title: '网络连接错误'
									});
								},
							});
						}
					}
				});	
			},
			/* 关闭sheet */
			sheetClose(){
				this.sexShow=false
				this.eduShow=false
			},
			/* 性别点击 */
			sexSelectClick(index){
				this.userData.userSex=index.sexValue
			},
			/* 学历点击 */
			eduSelectClick(index){
				this.edu=index.name
				this.userData.userEdu=index.eduId
			},
			/* 修改信息 */
			saveData(){
				this.$axios.post(this.usersUrl.editUserUrl,this.userData).then((res)=>{
					if(res.data.success){
						uni.setStorageSync('user',this.userData)
						uni.showToast({
							icon: 'success',
							position: 'center',
							title: '修改成功'
						});
					}else{
						uni.showToast({
							icon: 'exception',
							position: 'center',
							title: '修改失败'
						});
					}
				})
				if(this.stuData.id!=undefined){
					this.stuData.stuPhoto=this.userData.userPhoto
					this.stuData.stuUsername=this.userData.userUname
					this.stuData.stuPassword=this.userData.userPwd
					this.stuData.stuPhone=this.userData.userPhone
					this.stuData.stuSex=this.userData.userSex
					this.stuData.stuAge=this.userData.userAge
					this.stuData.educationId=this.userData.userLabel
					this.$axios.post(this.usersUrl.editStuUrl,this.stuData).then((res)=>{
						if(res.data.success){
							uni.setStorageSync('stu',this.stuData)
							uni.showToast({
								icon: 'success',
								position: 'center',
								title: '修改成功'
							});
						}else{
							uni.showToast({
								icon: 'exception',
								position: 'center',
								title: '修改失败'
							});
						}
					})
				}
			},
		}
	}
</script>

<style>
	
</style>
